package com.imooc.coupon.constant;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * 优惠券分类枚举
 * Created By TINY
 * Create Time 2020/2/9 - 15:08
 **/
@Getter
@AllArgsConstructor
public enum CouponCategory {

    FULL_REDUCTION("满减券", "001"),
    DISCOUNT("折扣券", "002"),
    REDUCTION("立减券", "003");

    /** 优惠券分类描述 */
    private String description;

    /** 优惠券分类代码 */
    private String code;

    /**
     * 根据 code 取得相应的枚举
     * @param code code
     * @return 枚举对象
     */
    public static CouponCategory of(String code) {
        Objects.requireNonNull(code);

        return Stream.of(values()).filter(value -> value.code.equals(code))
                .findAny().orElseThrow(
                        () -> new IllegalArgumentException(
                                code + " 对应的优惠券分类不存在"));
    }
}
